<?php
	$airmonths = array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	$oldmovie = "";
	$olddir = "";
	$tvxml = "";
	$imdbnr = 0;
	$movtypes = array("mov","avi","mpg","mpeg","divx","ogm","mkv","mp4","m4v","mp4v");

	if(isset($argv[1]) and file_exists($argv[1]) and is_dir($argv[1])){
		processdirectory($argv[1]);
	}else{
		die("\nUsage: php series.php <directory>\n\n");
	}



	function processdirectory($dir){
		global $airmonths, $oldmovie, $movtypes, $tvxml, $olddir, $imdbnr;
	
		$d = opendir($dir);
		while(false !== ($file = readdir($d))){
			if(substr($file, 0, 1) == "."){ continue; }
			if(is_dir($dir.$file."/")){
				processdirectory($dir.$file."/");
				continue;
			}
			$ext = strtolower(substr($file, strrpos($file, ".")+1));
			if(!in_array($ext, $movtypes)){ continue; }
			if(file_exists($dir.str_replace(".".$ext,".srt", $file))){ echo "$file already has subtitle...\n"; continue; }
			if(file_exists($dir.str_replace(".".$ext,".txt", $file))){ echo "$file already has subtitle...\n"; continue; }
			echo "Processing file ".$file."\n";


			$matches = array();
			if(preg_match("/(.+?)\Ds(\d+)\.?e(\d+?)\.(\D+)\./i", $file, $matches)){
//				echo "version 1...\n";
				$episode = round($matches[3]);
				$season  = round($matches[2]);
				$release = $matches[4];
				$title   = str_replace("."," ",$matches[1]);
			}elseif(preg_match("/(.+?)\Ds(\d+)\.?x(\d+?)\.(\D+)\./i", $file, $matches)){
//				echo "version 2...\n";
				$episode = round($matches[3]);
				$season  = round($matches[2]);
				$release = $matches[4];
				$title   = str_replace("."," ",$matches[1]);
			}elseif(preg_match("/(.+?)\D(\d+)x(\d+)\.(\D+)\./i", $file, $matches)){
//				echo "version 3...\n";
				$episode = round($matches[3]);
				$season  = round($matches[2]);
				$release = $matches[4];
				$title   = str_replace("."," ",$matches[1]);
			}elseif(preg_match("/(.+?)\D(\d+)\.(\D+)\./", $file, $matches)){
//				echo "version 5...\n";
//				print_r($matches);
				$episode = round(substr($matches[2],1));
				$season  = round(substr($matches[2],0,1));
				$release = $matches[3];
				$title   = str_replace("."," ",$matches[1]);
			}elseif(preg_match("/(\d+?) - ([\w\s]+) - /", $file, $matches)){
//				echo "version 6...\n";
				$episode = round(substr($matches[1],1));
				$season  = 1;
				$title   = str_replace("."," ",$matches[2]);
				$release = "none";
			}else{
				echo "Sorry, interpretion not possible\n";
				continue;
			}

			echo "Season: $season\nEpisode: $episode\nRelease: $release\n";

			$suburl = "http://www.podnapisi.net/index.php/ppodnapisi/search?tbsl=3&sK=".$title."&sJ=2&sT=1&sTS=".$season."&sTE=".$episode; 
			// ."&sR=".$release;
//			echo "Subtitle search at : ".$suburl."\n";
			$subfile = implode("",file($suburl));
	
			$fileparts = array();
			preg_match_all("/download\?i\=(\d+)\".+?<div align=\"left\">Release: (.+?)<\/div>/",$subfile,$fileparts);

			for($i=0;$i<count($fileparts[0]);$i++){
		
				$fndparts = preg_split("/[\W\_]/", strtolower($fileparts[2][$i]));
				$relparts = preg_split("/[\W\_]/", strtolower($release));


				if(count(array_diff($fndparts, $relparts)) < count($fndparts)/2){
		
					echo "Found match... ".$fileparts[1][$i]."\n";
					$srtfile = implode("",file("http://www.podnapisi.net/index.php/ppodnapisi/download?i=".$fileparts[1][$i]));
					$h = fopen("/tmp/seriesub.zip","w");
					fwrite($h, $srtfile);
			
					$zip = array();
					exec("unzip -nj /tmp/seriesub.zip -d /tmp/", $zip);

					foreach($zip as $zfile){
						$matches = array();
						if(preg_match("/inflating\:\s*(.+?\.srt)/", $zfile, $matches)){
							exec("cp '".$matches[1]."' '".$dir.str_replace(".".$ext,".srt", $file)."'");
							exec("rm '".$matches[1]."'");
							exec("rm /tmp/seriesub.zip");
							echo "Subtitle installed...\n\n";
							break 2;
						}elseif(preg_match("/inflating\:\s*(.+?\.txt)/", $zfile, $matches)){
								exec("cp '".$matches[1]."' '".$dir.str_replace(".".$ext,".txt", $file)."'");
								exec("rm '".$matches[1]."'");
								exec("rm /tmp/seriesub.zip");
								echo "Subtitle installed...\n\n";
								break 2;
						
						}
					}

				}else{
			
					echo $fileparts[2][$i]." does not fit ".$release."\n";
			
				}
			}
		}
	}
	exit;



function get_show($show,$exact="",$episode="") {

if ( !$show ) { return FALSE; }

if ( $fp = fopen("http://www.tvrage.com/quickinfo.php?show=".urlencode($show)."&ep=".urlencode($episode)."&exact=".urlencode($exact),"r") )
  {
  while ( !feof($fp))
    {
    $line = fgets($fp,1024);
    list ($sec,$val) = explode('@',$line,2);
    if ($sec == "Show Name" )
      {
      $ret[0] = $val;
      }
    elseif ( $sec == "Show URL" )
      {
      $ret[1] = $val;
      }
    elseif ( $sec == "Premiered" )
      {
      $ret[2] = $val;
      }
    elseif ($sec == "Country" )
      {
      $ret[7] = $val;
      }
    elseif ( $sec == "Status" )
      {
      $ret[8] = $val;
      }
    elseif ( $sec == "Classification" )
      {
      $ret[9] = $val;
      }
    elseif ( $sec == "Genres" )
      {
      $ret[10] = $val;
      }
    elseif ( $sec == "Network" )
      {
      $ret[11] = $val;
      }
    elseif ( $sec == "Airtime" )
      {
      $ret[12] = $val;
      }

    elseif ( $sec == "Latest Episode" )
      {
      list ($ep,$title,$airdate) = explode('^',$val);
      $ret[3] = $ep.", \"".$title."\" aired on ".$airdate;
      }
    elseif ( $sec == "Next Episode" )
      {
      list ($ep,$title,$airdate) = explode('^',$val);
      $ret[4] = $ep.", \"".$title."\" airs on ".$airdate;
      }
    elseif ( $sec == "Episode Info" )
      {
      list ($ep,$title,$airdate) = explode('^',$val);
      $ret[5] = $ep.", \"".$title."\" aired on ".$airdate;
      }
    elseif ( $sec == "Episode URL" )
      {
      $ret[6] = $val;
      }
    }
  fclose($fp);
  if ( $ret[0] )
    {
    return $ret;
    }
  }
else
  {
  return FALSE;
  }
}


?>
